package edu.umn.msse.farside.FourH.web;

import edu.umn.msse.farside.FourH.domain.Event;
import edu.umn.msse.farside.FourH.domain.Position;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindingResult;

public class PositionValidator
{
    public PositionValidator()
    {
    }

    public void validate(Position position, BindingResult result, Event event)
    {
        // any validation checking

        // required fields
        if (StringUtils.isBlank(position.getName()))
        {
            result.rejectValue("name", "required", "Required");  // property resource
        }

        if (StringUtils.isBlank(position.getDescription()))
        {
            result.rejectValue("description", "required", "Required");
        }

        if (0 == position.getNumberOfVolunteers())
        {
            result.rejectValue("numberOfVolunteers", "required", "Required");
        }

        // not required, but needs to be valid if supplied
        if ((null != position.getStartDateAndTime()) && (null != position.getEndDateAndTime()))
        {
            if (!position.getStartDateAndTime().before(position.getEndDateAndTime()))
            {
                result.rejectValue("endDateAndTime", "invalid", "Must be after start date");
            }
        }

        if (position.getStartDateAndTime() != null)
        {
            if (position.getStartDateAndTime().before(event.getStartDateAndTime()) ||
                position.getStartDateAndTime().after(event.getEndDateAndTime()))
            {
                result.rejectValue("startDateAndTime", "invalid", "Must be within the event period");
            }
        }

        if (position.getEndDateAndTime() != null)
        {
            if (position.getEndDateAndTime().before(event.getStartDateAndTime()) ||
                position.getEndDateAndTime().after(event.getEndDateAndTime()))
            {
                result.rejectValue("endDateAndTime", "invalid", "Must be within the event period");
            }
        }
    }
}